package tlv;

import java.util.ArrayList;
import java.util.List;

public class TlvBean {
	private int ver;
	private int type;
	private int papChap = 0;
	private int rsvd = 0;
	private int serialNo;
	private int reqId;
	private String userIp;
	private int userPort = 0;
	private int errCode;
	private int attrNum;
	private List<Attr> attr = new ArrayList<Attr>();

	public int getVer() {
		return ver;
	}

	public void setVer(int ver) {
		this.ver = ver;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getSerialNo() {
		return serialNo;
	}

	public void setSerialNo(int serialNo) {
		this.serialNo = serialNo;
	}

	public int getReqId() {
		return reqId;
	}

	public void setReqId(int reqId) {
		this.reqId = reqId;
	}

	public String getUserIp() {
		return userIp;
	}

	public void setUserIp(String userIp) {
		this.userIp = userIp;
	}

	public int getErrCode() {
		return errCode;
	}

	public void setErrCode(int errCode) {
		this.errCode = errCode;
	}

	public int getAttrNum() {
		return attrNum;
	}

	public void setAttrNum(int attrNum) {
		this.attrNum = attrNum;
	}

	public List<Attr> getAttr() {
		return attr;
	}

	public void setAttr(List<Attr> attr) {
		this.attr = attr;
	}

	public TlvBean() {
	}

	public TlvBean(int ver, int type, int serialNo, int reqId, String userIp, int errCode, int attrNum, List<Attr> attr) {
		super();
		this.ver = ver;
		this.type = type;
		this.serialNo = serialNo;
		this.reqId = reqId;
		this.userIp = userIp;
		this.errCode = errCode;
		this.attrNum = attrNum;
		this.attr = attr;
	}

	public TlvBean(int ver, int type, int papChap, int rsvd, int serialNo, int reqId, String userIp, int userPort,
			int errCode, int attrNum, List<Attr> attr) {
		super();
		this.ver = ver;
		this.type = type;
		this.papChap = papChap;
		this.rsvd = rsvd;
		this.serialNo = serialNo;
		this.reqId = reqId;
		this.userIp = userIp;
		this.userPort = userPort;
		this.errCode = errCode;
		this.attrNum = attrNum;
		this.attr = attr;
	}

	public byte[] toByteArray() {
		int dLen = 16;
		if (!(attr==null&&attr.isEmpty())) {
			for (Attr a : attr) {
				dLen += a.getAttrLen();
			}
		}
		byte[] b = new byte[dLen];
		b[0] = (byte) ver;
		b[1] = (byte) type;
		b[2] = (byte) papChap;
		b[3] = (byte) rsvd;
		b[4] = (byte) (serialNo & 0xff);
		b[5] = (byte) (serialNo >> 8 & 0xff);
		b[6] = (byte) (reqId & 0xff);
		b[7] = (byte) (reqId >> 8 & 0xff);
		byte[] ip = ipv4Address2BinaryArray(userIp);
		System.arraycopy(ip, 0, b, 8, 4);
		b[12] = (byte) (userPort & 0xff);
		b[13] = (byte) (userPort >> 8 & 0xff);
		b[14] = (byte) errCode;
		b[15] = (byte) attrNum;
		// 生成附加属性
		int t1 = 16;
		if (!(attr==null&&attr.isEmpty())) {
			for (Attr a : attr) {
				b[t1] = (byte) a.getAttrType();
				b[t1 + 1] = (byte) a.getAttrLen();
				System.arraycopy(a.getAttrValue().getBytes(), 0, b, t1 + 2, a.getAttrValue().getBytes().length);
				t1 += a.getAttrLen();
			}
		}
//		byte[] au = getAuthenticator(b);
//		System.arraycopy(au, 0, b, 16, 16);
		return b;
	}

	private byte[] ipv4Address2BinaryArray(String ipAdd) {
		byte[] binIP = new byte[4];
		String[] strs = ipAdd.split("\\.");
		for (int i = 0; i < strs.length; i++) {
			binIP[i] = (byte) Integer.parseInt(strs[i]);
		}
		return binIP;
	}
}
